﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.Database;
using TheDrinksApp.Models;
using TheDrinksApp.Screen.RecentOrder.TableViewCell;
using System.Collections.ObjectModel;

namespace TheDrinksApp.Screen.RecentOrder
{
    public partial class RecentOrderScreen : UserControl
    {
        public MainPage parent;
        public RecentOrderScreen(MainPage _parent)
        {
            InitializeComponent();
            parent = _parent;
        }

        public void loadAllRecentOrders()
        {
            listControls.Items.Clear();
            List<TheDrinksApp.Models.Order> list = Util.GetAllOrder().ToList();
            foreach (var order in list)
            {
                listControls.Items.Add(new RecentOrderRow(this, order));
            }
        }

        public void removeItem(UIElement item)
        {
            listControls.Items.Remove(item);
        }

        public void showOrderDetails( Models.Order order )
        {
            Util.orderEditting = order;
            Util.goFromRecentOrCurrent = 1;
            Util.HowMuchRecent = order.HowMuch;
            Util.recentOrders.Clear();
            ObservableCollection<OrderItem> list = new ObservableCollection<OrderItem>();
            list = Util.GetAllOrderItemByOrderID(order.OrderID);
            foreach (OrderItem item in list)
            {
                Util.recentOrders.Add(Util.ConvertOderItemToCurrentOrderDAO(item));
            }
            parent.ShowPage(PAGE.SUMARY_SCREEN);
            showAllRenameButton();
        }

        public void showAllRenameButton()
        {
            foreach (var item in listControls.Items)
            {
                ((RecentOrderRow)item).showRenameButton();
            }
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            loadAllRecentOrders();
        }
    }
}
